package co.fitstart.weapp.api.web.controller.profile;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.result.HttpResult;

import co.fitstart.entity.user.User;
import co.fitstart.entity.user.inbody.InbodyRecord;
import co.fitstart.weapp.api.service.UserService;

/**
 * 
 * @author dan
 *
 */
@RestController
@RequestMapping("/profile")
public class ProfileFitController {
    
    @Autowired
    private UserService userService;
    
    
    /**
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(path = "/inbody", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult index(@RequestParam("unionid") String unionid) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            
            InbodyRecord inbodyRecord = userService.getLatestInbodyRecord(user);
            httpResult.success().message("获取身体数据成功")
                                .data("inbodyRecord", inbodyRecord);
            
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(path = "/inbody/records", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult inbodyRecord(@RequestParam("unionid") String unionid) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            List<InbodyRecord> records = userService.listInbodyRecord(user);
            httpResult.success().message("获取数据成功").data("records", records);
            
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }
    
}
